:: Copyright (C) 2017-2024 Intel Corporation
:: SPDX-License-Identifier: MIT
@echo off
setlocal
set etw_list=%~1
set out_dir=%~2
set etl_file=%~3

if "%etw_list%"=="" goto usage
if "%out_dir%"=="" goto usage
if not "%~4"=="" goto usage
goto argsok
:usage
    echo usage: collect_etw_info.cmd path\to\etw_list.exe output_dir [etl_file]
    exit /b 1
:argsok

if not exist "%etw_list%" (
    echo error: etw_list path does not exist: %etw_list%
    goto usage
)

if not exist "%out_dir%\." (
    echo error: output_dir path does not exist: %out_dir%
    goto usage
)

if not "%etl_file%"=="" if not exist "%etl_file%" (
    echo error: etl_file path does not exist: %etl_file%
    goto usage
)

set events=
set events=%events% --event=Present::Start
set events=%events% --event=Present::Stop
call :etw_list "Microsoft-Windows-D3D9" "%out_dir%\Microsoft_Windows_D3D9.h"

set events=
set events=%events% --event=MILEVENT_MEDIA_UCE_PROCESSPRESENTHISTORY_GetPresentHistory::Info
set events=%events% --event=SCHEDULE_PRESENT::Start
set events=%events% --event=SCHEDULE_SURFACEUPDATE::Info
call :etw_list "Microsoft-Windows-Dwm-Core" "%out_dir%\Microsoft_Windows_Dwm_Core.h"

set events=
set events=%events% --event=Present::Start
set events=%events% --event=Present::Stop
set events=%events% --event=PresentMultiplaneOverlay::Start
set events=%events% --event=PresentMultiplaneOverlay::Stop
call :etw_list "Microsoft-Windows-DXGI" "%out_dir%\Microsoft_Windows_DXGI.h"

set events=
set events=%events% --event=AdapterAllocation::Start
set events=%events% --event=AdapterAllocation::Stop
set events=%events% --event=AdapterAllocation::DCStart
set events=%events% --event=Blit::Info
set events=%events% --event=BlitCancel::Info
set events=%events% --event=Flip::Info
set events=%events% --event=FlipMultiPlaneOverlay::Info
set events=%events% --event=IndependentFlip::Info
set events=%events% --event=HSyncDPCMultiPlane::Info
set events=%events% --event=VSyncDPCMultiPlane::Info
set events=%events% --event=MMIOFlip::Info
set events=%events% --event=MMIOFlipMultiPlaneOverlay::Info
set events=%events% --event=MMIOFlipMultiPlaneOverlay3::Info
set events=%events% --event=Present::Info
set events=%events% --event=PresentHistory::Start
set events=%events% --event=PresentHistory::Info
set events=%events% --event=PresentHistoryDetailed::Start
set events=%events% --event=QueuePacket::Start
set events=%events% --event=QueuePacket::Start_2
set events=%events% --event=QueuePacket::Stop
set events=%events% --event=VSyncDPC::Info
set events=%events% --event=Context::DCStart
set events=%events% --event=Context::Start
set events=%events% --event=Context::Stop
set events=%events% --event=HwQueue::DCStart
set events=%events% --event=HwQueue::Start
set events=%events% --event=NodeMetadata::Info
set events=%events% --event=Device::DCStart
set events=%events% --event=Device::Start
set events=%events% --event=Device::Stop
set events=%events% --event=DmaPacket::Start
set events=%events% --event=DmaPacket::Info
call :etw_list "Microsoft-Windows-DxgKrnl" "%out_dir%\Microsoft_Windows_DxgKrnl.h"

set events=
set events=%events% --event=ProcessStart::Start
set events=%events% --event=ProcessStop::Stop
call :etw_list "Microsoft-Windows-Kernel-Process" "%out_dir%\Microsoft_Windows_Kernel_Process.h"

set events=
set events=%events% --event=TokenCompositionSurfaceObject::Info
set events=%events% --event=TokenStateChanged::Info
set events=%events% --event=InputDeviceRead::Stop
set events=%events% --event=RetrieveInputMessage::Info
call :etw_list "Microsoft-Windows-Win32k" "%out_dir%\Microsoft_Windows_Win32k.h"

set events=
set events=%events% --event=FlipFrameType::Info
set events=%events% --event=PresentFrameType::Info
call :etw_list "Intel-PresentMon" "%out_dir%\Intel_PresentMon.h"

echo %out_dir%\NT_Process.h
echo // Copyright ^(C^) 2020-2022 Intel Corporation> "%out_dir%\NT_Process.h"
echo // SPDX-License-Identifier: MIT>> "%out_dir%\NT_Process.h"
echo //>> "%out_dir%\NT_Process.h"
echo // This file originally generated by run_etw_list.cmd>> "%out_dir%\NT_Process.h"
echo //>> "%out_dir%\NT_Process.h"
echo #pragma once>> "%out_dir%\NT_Process.h"
echo.>> "%out_dir%\NT_Process.h"
echo namespace NT_Process {>> "%out_dir%\NT_Process.h"
echo.>> "%out_dir%\NT_Process.h"
echo struct __declspec^(uuid^("{3d6fa8d0-fe05-11d0-9dda-00c04fd7ba7c}"^)^) GUID_STRUCT;>> "%out_dir%\NT_Process.h"
echo static const auto GUID = __uuidof^(GUID_STRUCT^);>> "%out_dir%\NT_Process.h"
echo.>> "%out_dir%\NT_Process.h"
echo }>> "%out_dir%\NT_Process.h"

exit /b 0

:etw_list
    echo %~2
    if "%etl_file%"=="" (
        "%etw_list%" --no_event_structs %events% --provider=%~1 >%2
    ) else (
        "%etw_list%" --no_event_structs %events% --provider=%~1 --etl="%etl_file%" >%2
    )
    exit /b 0

